package com.school.view;

import com.school.domain.Course;
import com.school.domain.Grade;
import com.school.domain.Student;
import com.school.proxy.MyProxy;
import com.school.service.StudentService;

import java.util.List;
import java.util.Scanner;

public class StudentView {
    public long sid;
    private final StudentService studentService = (StudentService) MyProxy.getProxy(StudentService.class, "127.0.0.1", 10000);

    Scanner scanner = new Scanner(System.in);

    public StudentView() {
    }

    public void studentView() {
        Scanner scanner = new Scanner(System.in);
        login();
        boolean flag = true;
        while (flag) {
            System.out.println("1.查看个人信息\t2.查看同学信息\t3.选课\t4.查分\t5.占座\t6.修改个人信息\t7.所占座位\t8.登出");
            int chose = scanner.nextInt();
            switch (chose) {
                case 1:
                    showInfo();
                    break;
                case 2:
                    findClassmates();
                    break;
                case 3:
                    choseCourse();
                    break;
                case 4:
                    selectGrade();
                    break;
                case 5:
                    occupySeat();
                    break;
                case 6:
                    changeInfo();
                    break;
                case 7:
                    showClassRoom();
                    break;
                case 8:
                    flag = false;
                    break;
                default:
                    System.out.println("请输入正确选项！");
                    break;
            }
        }
    }

    public void login() {
        while (true) {
            System.out.println("请输入账户密码：");
            long id = scanner.nextLong();
            String pwd = scanner.next();
            if (studentService.studentLogin(id, pwd)){
                sid = id;
                break;
            }
            System.out.println("账户或密码错误！");
        }

    }

    public void selectGrade() {
        List<Grade> grand = studentService.findGrand(sid);
        for (Grade grade : grand) {
            System.out.println(grade);
        }
    }

    public void showInfo() {
        System.out.println(studentService.find(sid));
    }

    public void changeInfo() {
        Student student = studentService.find(sid);
        System.out.print("输入姓名：");
        student.setName(scanner.next());
        System.out.print("输入密码：");
        student.setPwd(scanner.next());
        System.out.print("输入年龄：");
        student.setAge(scanner.nextLong());
        System.out.print("输入性别：");
        student.setSex(scanner.next());
        studentService.update(student);

    }

    public void findClassmates() {
        List<Student> classMates = studentService.findClassMates(studentService.find(sid).getClid());
        for (Student classMate : classMates) {
            if (classMate.getSid() == sid) continue;
            System.out.println(classMate);
        }
    }

    public void choseCourse() {
        System.out.println("所有课程：");
        List<Course> courses = studentService.findAllCourses();
        for (Course cours : courses) {
            System.out.println(cours);
        }
        System.out.println("输入要选的课程编号");
        if (studentService.choseCourse(sid, scanner.nextLong())) {
            System.out.println("选课成功!");
        } else {
            System.out.println("选课失败！");
        }
        System.out.println("------------------------------------------------");
        System.out.println("已选课程：");
        List<Course> courseList = studentService.findCourses(sid);
        for (Course course : courseList) {
            System.out.println(course);
        }
    }


    public void occupySeat() {
        System.out.println("输入要占座的教室编号：");
        if (studentService.choseClassRoom(studentService.find(sid), scanner.nextLong())) {
            System.out.println("占座成功!");
            return;
        }
        System.out.println("占座失败！");
    }

    public void showClassRoom() {
        System.out.println(studentService.findClassRoom(studentService.find(sid).getRid()));
    }
}
